import numpy as np

class TimeSeries:

	def __init__(self, time_list, security_list, field_list = None, value_array = None):
	
		self.ts_time_list = time_list
		self.ts_security_list = security_list
		self.ts_field_list = field_list
		self.ts_value_array = value_array #type is 3-d array with [time, security, field]
	
	def add_field(self, field_list, value_array):
		
		if self.ts_field_list:
			self.ts_field_list += field_list
		else:
			self.ts_field_list = field_list
			
		if value_array.ndim < 3:
			value_array = value_array.reshape((len(self.ts_time_list), len(self.ts_security_list), 1))
	
		if self.ts_value_array is not None:
			self.ts_value_array = np.concatenate((self.ts_value_array, value_array), axis=2)
		else:
			self.ts_value_array = value_array
			
	def del_field(self, field_list):	
		
		if field_list:
			for field in field_list:
				field_index = self.field_index([field])[0]
				
				if field_index is not None:
					del self.ts_field_list[field_index]
					self.ts_value_array = np.delete(self.ts_value_array, field_index, axis=2)
					
	def time_list(self):
		return self.ts_time_list
	
	def security_list(self):	
		return self.ts_security_list
		
	def field_list(self):
		return self.ts_field_list	
		
	def time_index(self, time_list):
	
		if time_list:
			for t in time_list:
				if not (t in self.ts_time_list):
					print 'Warn - time is not found - ' + t.isoformat()
					return
					
			return [self.ts_time_list.index(t) for t in time_list]
			
	def security_index(self, security_list):
		
		if security_list:
			for s in security_list:
				if not (s in self.ts_security_list):
					print 'Warn - security is not found - ' + s 
					return
					
			return [self.ts_security_list.index(s) for s in security_list]
			
	def field_index(self, field_list):
	
		if field_list:
			for f in field_list:
				if not (f in self.ts_field_list):
					print 'Warn - field is not found - ' + f  
					return
					
			return [self.ts_field_list.index(f) for f in field_list]
		
	def value_array(self, time_list = None, security_list = None, field_list = None):
		
		t_index = self.time_index(time_list)
		s_index = self.security_index(security_list)
		f_index = self.field_index(field_list)
		
		if t_index:
			if s_index:
				if f_index:
					slice_dim1 = self.ts_value_array[t_index, :, :]
					slice_dim2 = slice_dim1[:, s_index, :]
					return slice_dim2[:, :, f_index]
				else:
					slice_dim1 = self.ts_value_array[t_index, :, :]
					return slice_dim1[:, s_index, :]
			else:
				if f_index:
					slice_dim1 = self.ts_value_array[t_index, :, :]
					return slice_dim1[:, :, f_index]
				else:
					return self.ts_value_array[t_index, :, :]
		else:
			if s_index:
				if f_index:
					slice_dim2 = self.ts_value_array[:, s_index, :]
					return slice_dim2[:, :, f_index]
				else:
					return self.ts_value_array[:, s_index, :] 
			else:
				if f_index:
					return self.ts_value_array[:, :, f_index]
				else:
					return self.ts_value_array
					
if __name__ == '__main__':
	
	time_list = ['t1', 't2', 't3', 't4']
	security_list = ['s1', 's2', 's3']
	field_list = ['f1', 'f2']
	v = np.array([[[1,2],[3,4],[5,6]],
				 [[7,8],[9,10],[11,12]],
				 [[13,14],[15,16],[17,18]],
				 [[19,20],[21,22],[23,24]]])
	
	ts = TimeSeries(time_list, security_list, field_list, v)
	
	print ts.value_array(time_list = ['t1','t3'], field_list = ['f1','f2'])
	
	v = ts.value_array()
	q1 = v[[0,2],:,:]
	q2 = q1[:,:,:]
	q3 = q2[:,:,[0,1]]
	
	print q3
	
	
	
	
	
	
	